package com.szpt.mo.bhubackground.controller;

import com.szpt.mo.bhubackground.entity.TbQuestion;
import com.szpt.mo.bhubackground.service.ITbQuestionService;
            import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import com.szpt.mo.bhubackground.entity.parameters.R_Result;
import com.szpt.mo.bhubackground.util.config.JWTUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import javax.servlet.http.HttpServletRequest;


/**
 * @Package: com.szpt.mo.bhubackground.entity.TbQuestion
 * @Description: <前端控制器>
 * @Author: mo
 * @CreateDate: 2022-06-06
 * @UpdateUser: mo
 * @UpdateDate: 2022-06-06
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
    @Api(tags = "TbQuestion-相关接口")
    @RequestMapping(value = "TbQuestion")
public class TbQuestionController {

private static final Logger logger=LoggerFactory.getLogger(TbQuestionController.class);

@Autowired
private ITbQuestionService service;

@GetMapping("{id}")
@ApiOperation(value = "获取某一实体")
@ResponseBody
public TbQuestion getITbQuestionServiceDetails(@PathVariable Long id){
        return service.getById(id);
        }

@PostMapping
@ApiOperation(value = "新增数据")
@ResponseBody
public boolean saveITbQuestionService(@RequestBody TbQuestion dto){
        return service.save(dto);
        }

@PutMapping("{id}")
@ApiOperation(value = "修改数据")
@ResponseBody
public boolean modifyITbQuestionService(@RequestBody TbQuestion dto,@PathVariable Long id){
        dto.setId(id);
        return service.updateById(dto);
        }

@DeleteMapping("batch")
@ApiOperation(value = "批量删除数据")
@ResponseBody
public boolean batchRemoveITbQuestionService(@RequestParam(value = "ids") List<Long> ids){
        return service.removeByIds(ids);
        }

@DeleteMapping("{id}")
@ApiOperation(value = "删除数据")
@ResponseBody
public boolean removeITbQuestionService(@PathVariable Long id){
        return service.removeById(id);
        }





@GetMapping("/getList/{current}/{size}/{total}")
@ApiOperation(value = " 获取列表数据")
@ResponseBody
public R_Result<List<TbQuestion>>addTbQuestion(HttpServletRequest request,@PathVariable Integer current,@PathVariable Integer size,@PathVariable Integer total){

        boolean save=false;
        R_Result<List<TbQuestion>>result=new R_Result<>();

        try{
        //String id = JWTUtils.getTokenPayload(request).getId();

        //添加查询条件
        QueryWrapper<TbQuestion> wrapper=new QueryWrapper<>();
        Page<TbQuestion> page=new Page<>(current,size,total);
        result.setData(service.page(page,wrapper).getRecords());


        save=true;

        }catch(Exception e){
        result.setMsg(e.getMessage());
        e.printStackTrace();
        }

        if(save){
        result.setCode(200);
        result.setMsg("成功");
        }else{
        result.setCode(250);
        result.setMsg("失败");
        }
        return result;
        }


@GetMapping("/add")
@ApiOperation(value = "TT 添加数据")
@ResponseBody
public R_Result<Boolean> addTbQuestion(HttpServletRequest request,@RequestBody TbQuestion dto){

        boolean save=false;
        R_Result<Boolean> result=new R_Result<>();

        //TbQuestion obj = new TbQuestion();
        try{
        //String id = JWTUtils.getTokenPayload(request).getId();

        // plansGroup.setName(name);

        //plansGroup.setUserId(Long.valueOf(id));

        //plansGroup.setAdminId(admin_id);

        save=service.save(dto);

        }catch(Exception e){
        result.setMsg(e.getMessage());
        e.printStackTrace();
        }

        if(save){
        result.setCode(200);
        result.setMsg("成功");
        }else{
        result.setCode(250);
        result.setMsg("失败");
        }
        result.setData(save);
        return result;
        }


@GetMapping("/edit")
@ApiOperation(value = "TT 编辑数据")
@ResponseBody
public R_Result<Boolean> editTbQuestion(HttpServletRequest request,@RequestBody TbQuestion otd){

        boolean save=false;
        R_Result<Boolean> result=new R_Result<>();
        try{

//            String userid = JWTUtils.getTokenPayload(request).getId();

        UpdateWrapper<TbQuestion> wrapper=new UpdateWrapper<>();

//            wrapper.eq("user_id", userid);
        wrapper.eq("id",otd.getId());
        //设置更新value
//            wrapper.set("name", name);


        save=service.update(otd,wrapper);

        }catch(Exception e){
        result.setMsg(e.getMessage());
        e.printStackTrace();
        }

        if(save){
        result.setCode(200);
        result.setMsg("成功");
        }else{
        result.setCode(250);
        result.setMsg("失败");
        }
        result.setData(save);
        return result;
        }



@DeleteMapping("/del/{id}")
@ApiOperation(value = "TT  条件删除数据")
@ResponseBody
public  R_Result<Boolean> delByTbQuestion(HttpServletRequest request,@PathVariable Long id){


        boolean save=false;
        R_Result<Boolean> result=new R_Result<>();

        try{
        String userid=JWTUtils.getTokenPayload(request).getId();
        QueryWrapper<TbQuestion> wrapper=new QueryWrapper<>();
        //wrapper.eq("user_id", userid);
        wrapper.eq("id",id);

        save=service.remove(wrapper);

        }catch(Exception e){
        result.setMsg(e.getMessage());
        e.printStackTrace();
        }

        if(save){
        result.setCode(200);
        result.setMsg("成功");
        }else{
        result.setCode(250);
        result.setMsg("失败");
        }
        result.setData(save);
        return result;

        }
        }
